﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace UcAsp.Net.BLL
{
    /// <summary>
    /// 内容过滤
    /// </summary>
    public class ContentFilter : Stream
    {

        Stream responseStream;
        long position;
        StringBuilder html = new StringBuilder();
      
        public ContentFilter(Stream inputStream)
        {

            responseStream = inputStream;
        }
        #region implemented abstract members

        public override bool CanRead
        {
            get { return true; }
        }

        public override bool CanSeek
        {
            get { return true; }
        }

        public override bool CanWrite
        {
            get { return true; }
        }

        public override void Close()
        {
            responseStream.Close();
        }

        public override void Flush()
        {
            responseStream.Flush();
        }

        public override long Length
        {
            get { return 0; }
        }

        public override long Position
        {
            get { return position; }
            set { position = value; }
        }

        public override long Seek(long offset, System.IO.SeekOrigin direction)
        {
            return responseStream.Seek(offset, direction);
        }

        public override void SetLength(long length)
        {
            responseStream.SetLength(length);
        }

        public override int Read(byte[] buffer, int offset, int count)
        {

            return responseStream.Read(buffer, offset, count);
        }

        #endregion

        #region write method

        public override void Write(byte[] buffer, int offset, int count)
        {
            string fileclass = "";
            byte[] ext = (byte[])buffer.Clone();
            try
            {
                fileclass = ext[0].ToString();
                fileclass += ext[1].ToString();
            }
            catch { }
            //说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar,|| fileclass=="13780"||fileclass=="6677"  
            if (fileclass == "255216" || fileclass == "7173" || fileclass == "13780" || fileclass == "6677")
            {
                responseStream.Write(buffer, 0, buffer.Length);
            }
            else
            {
                string sBuffer = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count);

                //得到非法词汇列表，这个可以在数据库或Web.Config中读取出来
                // string pattern = @"(非法词汇1|非法词汇2|非法词汇3)";


                // string[] s = pattern.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);

                //foreach (string s1 in s)
                // {
                //   sBuffer = sBuffer.Replace(s1, "**");
                //}


                byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(sBuffer);
                responseStream.Write(data, 0, data.Length);
            }
        }

        #endregion
    }
}
